package com.hyb.algorithm.data.struct.str;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: huyanbing
 * @Date: 2021/9/29 3:28 下午
 *
 * 242. 有效的字母异位词
 * https://leetcode-cn.com/problems/valid-anagram/
 *
 */
public class ValidAnagram {

    public boolean isAnagram(String s, String t) {

        //循环 遍历map

        int lenS=s.length();
        int lenT=t.length();

        if(lenS!=lenT){
            return false;
        }

        Map<Character,Integer> map=new HashMap<>();

        for(int i=0;i<lenT;i++){
            Character key=t.charAt(i);
            map.put(key,map.getOrDefault(key,0)+1);
        }

        for(int i=0;i<lenS;i++){
            Character key=s.charAt(i);
            int ret=map.getOrDefault(key,0)-1;
            if(ret<0){
                return false;
            }
            map.put(key,ret);
        }

        return true;

    }
}
